using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class Users
    {
          #region Common
        public eUsers   Data;

        public Users()
        {
            Data = new eUsers();
		}

        ~Users()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cUsers objDAL = new cUsers();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cUsers objDAL = new cUsers();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cUsers objInfor = new cUsers(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.UserID;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int Update(int ID)
        {
            int nResult = 0;
            cUsers objInfor = new cUsers();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int UpdateStatus(int ID, int Status)
        {
            int nResult = 0;
            cUsers objDAL = new cUsers();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.Status = Status;
               
                objDAL.Save();
                nResult = objDAL.UserID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        private void MappingData(cUsers objInfor)
        {

            objInfor.Username = Data.Username;
            objInfor.Password = Data.Password;
            objInfor.Email = Data.Email;
            objInfor.Status = Data.Status;
            objInfor.RoleID = Data.RoleID; 
           
        }

        private void ConvertData(cUsers objInfor)
        {
            Data.Username = objInfor.Username;
            Data.Password = objInfor.Password;
            Data.Email = objInfor.Email;
            Data.Status = objInfor.Status;
            Data.RoleID = objInfor.RoleID;
        }

      
        #endregion

        public DataTable Search(int ItemView, string Username, int Status, int RoleID, int PageIndex, int PageSize, ref int PageCount)
        {
            DataTable tbResult = new DataTable();
            cUsers objDAL = new cUsers();
            tbResult = objDAL.Search(ItemView, Username, Status, RoleID, PageIndex, PageSize, ref  PageCount);
            return tbResult;
        }
        public DataTable GetUserByEmail(string Email, int Status)
        {
            DataTable tbResult = new DataTable();
            cUsers objDAL = new cUsers();
            tbResult = objDAL.GetUserByEmail(Email, Status);
            return tbResult;
        }
    }
}
